<?php
   if(!defined('ROOT_PATH')){
		include('../public/404 Not Found.htm');
		exit();
	}


class NarynPHP{
	protected $g_group;
	protected $g_controller;
	protected $g_method;
	protected $classfile;
	protected $classname;
	
	function __construct() {
//	Import func config  and base class $url='',$appname=''

		include_once (ROOT_PATH.'NarynPHP/Common/func.php');
		include_once (ROOT_PATH.'NarynPHP/Config/config.php');
		include_once (ROOT_PATH.'NarynPHP/Lib/action.php');
		include_once (ROOT_PATH.'NarynPHP/Lib/model.php');
		include_once (ROOT_PATH.'NarynPHP/Lib/cache.php');
		

		if(DEBUG!='true'){
			error_reporting(E_ALL ^ E_NOTICE);
		}

		$this->init();

   }
   
	protected function init(){
	//get the web set info 
	if(!get_magic_quotes_gpc()){
		$_GET = ass($_GET);
		$_POST = ass($_POST);
		$_COOKIE = ass($_COOKIE);
		$_SESSION = ass($_SESSION);
		$_REQUEST = ass($_REQUEST);
		$_SERVER = ass($_SERVER);
		$_FILES = ass($_FILES);
	}

	$this->g_group	  = $this->get_g();
	$this->g_controller = $this->get_c();
	$this->g_method	  = $this->get_a();

	//set the current class info 
	define ('TPL_PATH',ROOT_PATH.'Templates/'.($this->g_group).'/');
	define ('__GROUP__',($this->g_group));
	$this->classfile = ROOT_PATH.'Controllers/'.($this->g_group).'/'.($this->g_controller).'Controller.class.php';
	$this->classname = $this->g_controller.'Controller';

	if(file_exists($this->classfile)){
		include($this->classfile);
	}else{
		$this->error('_ERROR: '.$this->g_group.'->'.$this->classname.' classfile is not exist !');
	}
	$this->run();

 } 
   
   
 	/*
		do the controller and method ;
	*/
 function run(){

	$class = $this->classname;
	$method = $this->g_method;
	$myclass = '';
	
	if (class_exists($class)) {
		$myclass = new $class();
	}else{
		$this->error('ERROR: the name of '.$class.' is not exist !');
	}
	if(method_exists ( $myclass , $method )){
		$myclass->$method();
	}else{
		$this->error('ERROR: the name of '.$class.'->'.$method.' method is not exist !');
	}
	/*
	try{
		$c->$method();
	}catch (Exception $e) { 
		print $e->getMessage(); 
		exit(); 
	} 
	*/
 }
   
/*
get the value of the URL controller
*/
function get_c(){
	$controller = $_GET['c'];
	$controller = $controller ? $controller:DEFAULT_C;//default controller
	return $controller;
}

/*
get the value of the URL action 
*/
function get_a(){
	$action = $_GET['a'];
	$action = $action ? $action : DEFAULT_A;//default action
	return $action;
}

/*
get the value of the URL group 
*/
function get_g(){
	$group = $_GET['g'];
	$group = $group ? $group : DEFAULT_G;//default group
	return $group;
}


/*
get the value of the URL parameter 
*/
function get_p($p){
	//$parameter = $_GET['p'];
	//return $parameter;
}

/*show error */
function error($msg='Error !',$state='0'){
	$data['msg'] = $msg; 
	$data['state']=$state;
	echo json_encode($data);
	exit;
}
//class end
}


?>
